﻿using Esst.Common;
using Esst.Common.Extensions;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Esst.Extensions.Setup
{
    /// <summary>
    /// Redis缓存 启动服务
    /// </summary>
    public static class RedisCacheSetup
    {
        public static void AddRedisCacheSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            services.AddTransient<IRedisRepository, RedisRepository>();

            services.AddSingleton<ConnectionMultiplexer>(sp =>
            {
                string redisConfiguration = AppSettings.App(["Redis", "ConnectionString"]);

                var configuration = ConfigurationOptions.Parse(redisConfiguration, true);
                configuration.ResolveDns = true;
                configuration.AbortOnConnectFail = false;
                configuration.SyncTimeout = AppSettings.App(["Redis", "Timeout"]).ToInt();

                return ConnectionMultiplexer.Connect(configuration);
            });
        }
    }
}
